﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Atosenet.Data
{
    public abstract class MultiPayLoadItem : PayloadItem
    {
        #region 成员

        private PayloadItemCollection _payloads = new PayloadItemCollection();

        #endregion


        #region 初始化

        public MultiPayLoadItem(byte[] data)
            : base(data)
        { }

        public MultiPayLoadItem()
            : base()
        { }

        #endregion


        #region 公开函数

        public override string ToString(string para)
        {
            if (string.IsNullOrEmpty(para))
                return base.ToString(para);

            switch (para.ToUpper())
            {
                case "F":
                    return GetFullSubItmeName();
                default:
                    return base.ToString(para);
            }
        }

        #endregion


        #region 静态函数



        #endregion


        #region 核心函数

        protected override string GetDetailInfo()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0}[", ToString());

            for (int i = 0; i < SubPayloadItems.Count; i++)
            {
                sb.AppendFormat("{0},", SubPayloadItems[i].ToString("D"));
            }
            sb = sb.Remove(sb.Length - 1, 1);
            sb.Append("]");
            return sb.ToString();
        }

        protected override bool VerifyIntegrity(byte[] data)
        {
            return true;
        }

        private string GetFullSubItmeName()
        {
            if(SubPayloadItems.Count ==0)
                return string.Empty;

            PayloadItem item = SubPayloadItems[0];

            if (SubPayloadItems.TrueForAll(pi => { return pi.ToString() == item.ToString(); }))
            {
                return string.Format("{0}[{1}*{2}]", ToString(), item.ToString(), SubPayloadItems.Count.ToString());
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("{0}[", ToString());
                    
                for (int i = 0; i < SubPayloadItems.Count; i++)
                {
                    sb.AppendFormat("{0},", SubPayloadItems[i].ToString());
                }
                sb = sb.Remove(sb.Length - 1, 1);
                sb.Append("]");
                return sb.ToString();
            }
        }

        #endregion


        #region 属性

        public PayloadItemCollection SubPayloadItems
        {
            get { return _payloads; }
            protected set { _payloads = value; }
        }

        public override int DataLength
        {
            get { return (SubPayloadItems == null || SubPayloadItems.Count == 0) ? 1 : SubPayloadItems.Count * SubPayloadItems[0].DataLength + 1; }
        }

        public override bool IsFixedFormat
        {
            get { return false; }
        }

        #endregion


        #region 附加对象

        public class PayloadItemCollection : BaseCollection<PayloadItem>
        {
            public PayloadItemCollection()
                : base()
            { }
        }

        #endregion
    }
}
